package io.ede.modules.ede.controller;

import io.ede.common.utils.PageUtils;
import io.ede.common.utils.R;
import io.ede.common.validator.ValidatorUtils;
import io.ede.modules.ede.entity.SysToolEntity;
import io.ede.modules.ede.service.SysToolService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-09-20 11:30:22
 */
@RestController
@RequestMapping("ede/systool")
public class SysToolController {
    @Autowired
    private SysToolService sysToolService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ede:systool:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysToolService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ede:systool:info")
    public R info(@PathVariable("id") Integer id) {
        SysToolEntity sysTool = sysToolService.getById(id);

        return R.ok().put("sysTool", sysTool);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ede:systool:save")
    public R save(@RequestBody SysToolEntity sysTool) {
        sysTool.setValid(0);
        sysTool.setCreateTime(new Date());
        sysToolService.save(sysTool);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ede:systool:update")
    public R update(@RequestBody SysToolEntity sysTool) {
        ValidatorUtils.validateEntity(sysTool);
        sysTool.setUpdateTime(new Date());
        sysToolService.updateById(sysTool);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ede:systool:delete")
    public R delete(@RequestBody Integer[] ids) {
        sysToolService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 启用
     */
    @RequestMapping("/startUse")
    @RequiresPermissions("ede:systool:update")
    public R startUse(Integer id) {
        try {
            sysToolService.startUse(id);
        }catch (Exception e){
            return R.error("启用数据异常，请稍后再试！");
        }
        return R.ok();
    }

}
